//
//  DLSportPolyline.m
//  DLSport
//
//  Created by DongL on 2017/4/23.
//  Copyright © 2017年 DongL. All rights reserved.
//

#import "DLSportPolyline.h"

@implementation DLSportPolyline

+ (instancetype)polylineWithSourceLocation:(CLLocation *)source andDestLocation:(CLLocation *)dest {
    
    //构造折线数据对象
    CLLocationCoordinate2D commonPolylineCoords[2];
    //起点  上一次的终点
    commonPolylineCoords[0].latitude = source.coordinate.latitude;
    commonPolylineCoords[0].longitude = source.coordinate.longitude;
    //终点
    commonPolylineCoords[1].latitude = dest.coordinate.latitude;
    commonPolylineCoords[1].longitude = dest.coordinate.longitude;
    
    //构造折线对象
    DLSportPolyline *commonPolyline = [DLSportPolyline polylineWithCoordinates:commonPolylineCoords count:2];
    //计算速度
    [commonPolyline calculateSpeedWithSoure:source andDest:dest];
    
    return commonPolyline;
    
    
}

//计算速度
- (void)calculateSpeedWithSoure:(CLLocation *)source andDest:(CLLocation *)dest{
    //(起点速度+终点速度) * 0.5  m/s -> km/hour   /1000 * 60 * 60
    _speed = (source.speed + dest.speed) * 0.5 * 3.6;
    //    NSLog(@"%f", _speed);
    // Red 0~1   speed 0~30
    _storkeColor = [UIColor colorWithRed:(_speed * 0.033) green:1 blue:0 alpha:1];
    
    //计算时间差
    _time = [dest.timestamp timeIntervalSinceDate:source.timestamp];
    //计算距离
    _distance = [dest distanceFromLocation:source] * 0.001;
    
}

@end
